package com.pz.cloud.framework.stdGroup.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroup;
import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroupUser;
import com.pz.cloud.framework.business.stdGroup.service.IOrgStdGroupService;
import com.pz.cloud.framework.business.workflow.annotation.AssembleDealFlag;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.BaseController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.util.PermissionDataUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RequestMapping("org/stdGroup")
@RestController
@Api(tags = "学生组织表")
@Slf4j
public class OrgStdGroupController extends BaseController<OrgStdGroup, IOrgStdGroupService> {
    /**
     * 分页列表查询
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "学生组织-分页列表查询")
    @ApiOperation(value = "学生组织-分页列表查询", notes = "学生组织-分页列表查询")
    @GetMapping(value = "/list")
    @AssembleDealFlag
    @PermissionData(pageComponent = "organization/NewList")
    public Result<IPage<OrgStdGroup>> queryPageList(OrgStdGroup entity,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        Result<IPage<OrgStdGroup>> result = new Result<>();
        if(entity.getActStatus()!=null&&entity.getActStatus()==10){
            PermissionDataUtil.setPermissionData("organization/OrganizationMange");
        }
        QueryWrapper<OrgStdGroup> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        Page<OrgStdGroup> page = new Page<>(pageNo, pageSize);
        IPage<OrgStdGroup> pageList = service.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     * @return
     */
    @AutoLog(value = "学生组织-添加|编辑")
    @ApiOperation(value = "学生组织-添加|编辑", notes = "学生组织-添加|编辑")
    @PostMapping(value = "/add")
    public Result<OrgStdGroup> add(@RequestBody OrgStdGroup entity) {
        Result<OrgStdGroup> result = new Result<>();
        try {
            service.save(entity);
            result.success("保存成功！");
        } catch (Exception e) {
            if(e instanceof CustomRuntimeException){
                result.error500(e.getMessage());
            }else{
                result.error500("操作失败");
            }
            log.error(e.getMessage(), e);
        }
        return result;
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "学生组织-通过id删除")
    @ApiOperation(value = "学生组织-通过id删除", notes = "学生组织-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        service.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "学生组织-批量删除")
    @ApiOperation(value = "学生组织-批量删除", notes = "学生组织-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<OrgStdGroup> deleteBatch(@RequestParam(name = "ids") String ids) {
        Result<OrgStdGroup> result = new Result<OrgStdGroup>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            service.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "学生组织-通过id查询")
    @ApiOperation(value = "学生组织-通过id查询", notes = "职务表-通过id查询")
    @GetMapping(value = "/queryById")
//    @AssembleTaskInfo
    public Result<OrgStdGroup> queryById(@RequestParam(name = "id") String id) {
        Result<OrgStdGroup> result = new Result<OrgStdGroup>();
        OrgStdGroup OrgStdGroup = service.getById(id);
        if (OrgStdGroup == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(OrgStdGroup);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 导出excel
     *
     * @param request
     */
    @RequestMapping(value = "/exportXls")
    @PermissionData(pageComponent = "organization/NewList")
    public ModelAndView exportXls(HttpServletRequest request, OrgStdGroup bean) {
        return super.exportXls(request, bean, OrgStdGroup.class, "校级组织管理");
    }

}
